using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using cframework_xlua;
using System.ComponentModel;
using Newtonsoft.Json;
namespace cframework_xlua
{
    [CreateAssetMenu(menuName = "cframework_xlua/PublishUtil_Apk")]
    public class PublishUtil_Apk : PlatformPublishUtil
    {
        public override bool BuildAndPublish(BuildPlayerSetting setting, BuildPlayerSetting.PublishInfo publishInfo)
        {
            string outputAppPath = publishInfo.buildOutputPath + $"/{publishInfo.appName}_{setting.platform}_{setting.subPlatform}_{setting.publishType}";

            if (setting.platform == cframework_xlua.Platform.android)
            {
                if (!BuildApkFromProject(setting, publishInfo.outputProjectPath, outputAppPath))
                {
                    return false;
                }
            }
            

            return PublishApps(setting, outputAppPath);
        }


        static bool PublishApps(BuildPlayerSetting setting, string destDirPath)
        {
            string searchExtName = null;
            if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
            {
                searchExtName = "*.apk";
            }
            else if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS)
            {
                searchExtName = "*.ipa";
            }

            if (string.IsNullOrEmpty(searchExtName))
            {
                return false;
            }

            {
                string[] apkPaths = System.IO.Directory.GetFiles(destDirPath, searchExtName);
                string downloadUrl = null;
                foreach (var a in apkPaths)
                {
                    if (setting.channelUtil && setting.channelUtil.Deploy(setting, a, null, null, ref downloadUrl))
                    {
                        setting.StartHttpServer();
                    }
                }

                if (!string.IsNullOrEmpty(downloadUrl))
                {
                    Debug.Log("pgy url: " + " " + downloadUrl);
                    if (EditorUtility.DisplayDialog("download url", downloadUrl, "copy", "ok"))
                    {
                        GUIUtility.systemCopyBuffer = downloadUrl;
                    }
                }
            }

            return true;
        }

        static bool BuildApkFromProject(BuildPlayerSetting setting, string outputProjectPath, string destDirPath)
        {
            string ouputApkMiddleDir = "release";
            string param = "assembleRelease";
            if (setting.developmentBuild)
            {
                param = "assembleDebug";
                ouputApkMiddleDir = "debug";
            }

            if (!System.IO.Directory.Exists(destDirPath))
            {
                System.IO.Directory.CreateDirectory(destDirPath);
            }

            if (System.IO.File.Exists(outputProjectPath))
            {
                System.IO.File.Copy(outputProjectPath, destDirPath + "/" + System.IO.Path.GetFileName(outputProjectPath) + ".apk", true);
                return true;
            }
            else
            {
                string gradlePath = setting.deviceToolPaths.GetToolPath("GRADLE_PATH");

                string outputApkDir = outputProjectPath + $"/launcher/build/outputs/apk/{ouputApkMiddleDir}";

                var ret = CommandLineTool.RunCommand2(gradlePath, param, outputProjectPath);
                if (ret.ExitCode != 0)
                {
                    Debug.LogError("output: " + ret.StdOut);
                    Debug.LogError("error: " + ret.StdErr);
                    return false;
                }
                else
                {
                    string[] apkPaths = System.IO.Directory.GetFiles(outputApkDir, "*.apk");
                    foreach (var a in apkPaths)
                    {
                        System.IO.File.Copy(a, destDirPath + "/" + System.IO.Path.GetFileName(a), true);
                    }
                }

                return true;
            }
        }
    }
}